
//
//  HomeViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "HomeViewController.h"
#import "AboutProductViewController.h"
#import "BtnModel.h"

#import "TopViewModel.h"
#import "TopView.h"
#import "ProvTableViewController.h"
#import "RepeatViewCell.h"
#import "PublicBenefitViewController.h"
#import "TumourViewController.h"

#define margin 1
#define kBtnCount 3
#define kScreen [UIScreen mainScreen].bounds.size.width
@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
//
////顶部绿色view
//@property (nonatomic, strong) UIView *headView;

//绿色view中的灰色分割线
@property (nonatomic, strong) UIView *lineView;

//图片轮播 collectionView
@property (nonatomic, weak) UICollectionView *repeatCollectionView;

//设置flowLayout
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

//名医通view
@property (nonatomic, strong) UIView *marginView;

//名医通imageView
@property (nonatomic, strong) UIImageView *mv_imageView;

//名医通label
@property (nonatomic, strong) UILabel *label;

//底部view(6个button)
@property (nonatomic, strong) UIView *groupView;

//顶部view的数组
@property (nonatomic, strong) NSArray *topViewList;

@property (nonatomic, strong) TopViewModel *model;

//顶部view中imageView
@property (nonatomic, strong) UIImageView *topViewImageView;

//图片数组
@property (nonatomic, strong) NSArray *imageArray;

//全局成员属性 引用定时器
@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic, weak) UIPageControl *pageControl;

@end

@implementation HomeViewController

#pragma mark
#pragma mark -- 懒加载
- (UIView *)lineView{
    if (nil == _lineView) {
        _lineView = [[UIView alloc] init];
    }
    return _lineView;
}

- (NSArray *)imageArray{
    if (nil == _imageArray) {
        _imageArray = @[
                        @"http://img.guahao.com/portal_upload/rollingnews/1453447883150.jpg",
                        @"http://img.guahao.com/portal_upload/mediafocus/1461826474435.jpg",
                        @"http://img.guahao.com/portal_upload/rollingnews/1461574027543.jpg"
                        ];
    }
    return _imageArray;
}

- (UICollectionViewFlowLayout *)flowLayout{
    if (nil == _flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    }
    return _flowLayout;
}

- (UIView *)marginView{
    if (nil == _marginView) {
        _marginView = [[UIView alloc] init];
    }
    return _marginView;
}

- (UIView *)groupView{
    if (nil == _groupView) {
        _groupView = [[UIView alloc] init];
    }
    return _groupView;
}

- (UIImageView *)mv_imageView{
    if (nil == _mv_imageView) {
        _mv_imageView = [[UIImageView alloc] init];
    }
    return _mv_imageView;
}

- (UILabel *)label{
    if (nil == _label) {
        _label = [[UILabel alloc] init];
    }
    return _label;
}

- (void)setTopViewList:(NSArray *)topViewList{
    _topViewList = topViewList;
}

- (TopViewModel *)model{
    if (nil == _model) {
        _model = [[TopViewModel alloc] init];
    }
    return _model;
}

- (UIImageView *)topViewImageView{
    if (nil == _topViewImageView) {
        _topViewImageView = [[UIImageView alloc] init];
    }
    return _topViewImageView;
}

#pragma mark
#pragma mark -- viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(playPicture) userInfo:nil repeats:YES];
    
    //添加到运行循环里
    self.timer = timer;
    
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)playPicture{
    
    //1.拿到当前的偏移量
    CGFloat offsetX = self.repeatCollectionView.contentOffset.x;
    
    //2.修改偏移量
    offsetX += self.repeatCollectionView.bounds.size.width;
    
    //3.赋值
    [self.repeatCollectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [self setupFlowLayout];
    
    //初始化位置
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:1];
    [self.repeatCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

- (void)setupFlowLayout{
    
    self.flowLayout.itemSize = self.repeatCollectionView.frame.size;
    
    //    NSLog(@"%@",NSStringFromCGRect(self.repeatCollectionView.frame));
    
    //设置滚动方向
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    //间距
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    
    //分页
    self.repeatCollectionView.pagingEnabled = YES;
    
    self.repeatCollectionView.showsHorizontalScrollIndicator = NO;
    //弹簧
    self.repeatCollectionView.bounces = NO;
}

#pragma mark
#pragma mark -- 设置加载界面
- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = kThemeColor;
    
    //设置导航栏渲染为白色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    
    self.title = @"黑马快医";
#pragma
#pragma mark -- headView
    TopView *headView = [[TopView alloc] init];
    [self.view addSubview:headView];
    
    self.lineView.backgroundColor = [UIColor colorWithRed:0.6667 green:0.6667 blue:0.6667 alpha:1.0];
    
    [self.view addSubview:self.lineView];
    
#pragma
#pragma mark -- 图片轮播view
    UICollectionView *repeatCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    
    self.repeatCollectionView = repeatCollectionView;
    //    self.repeatCollectionView.backgroundColor = [UIColor redColor];
    repeatCollectionView.delegate = self;
    repeatCollectionView.dataSource = self;
    
    [self.repeatCollectionView registerClass:[RepeatViewCell class] forCellWithReuseIdentifier:@"repeat"];
    [self.view addSubview:self.repeatCollectionView];
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.502 alpha:1.0];
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    pageControl.numberOfPages = self.imageArray.count;
    //    pageControl.currentPage = 0;
    self.pageControl = pageControl;
    [self.view addSubview:pageControl];
    
#pragma
#pragma mark -- 名医通view
    [self.view addSubview:self.marginView];
    
    self.mv_imageView.image = [UIImage imageNamed:@"yj"];
    [self.marginView addSubview:self.mv_imageView];
    
    [self.label setText:@"名医通"];
    [self.label setFont:[UIFont systemFontOfSize:20]];
    [self.marginView addSubview:self.label];
    
#pragma
#pragma mark -- 底部组view
    self.groupView.backgroundColor = [UIColor colorWithRed:0.6667 green:0.6667 blue:0.6667 alpha:1.0];
    [self.view addSubview:self.groupView];
    
    //循环添加button
    [self addbtn];
    
#pragma
#pragma mark -- 设置约束
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(64);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(0.12);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(headView);
        make.width.equalTo(@1);
        make.height.equalTo(headView).multipliedBy(0.75);
    }];
    
    [self.repeatCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(headView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(0.3);
    }];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.marginView.mas_top).offset(5);
    }];
    
    [self.marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.repeatCollectionView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(0.08);
    }];
    
    [self.mv_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.marginView);
        make.leading.equalTo(self.marginView).offset(20);
        make.height.equalTo(self.marginView).multipliedBy(0.5);
        make.width.equalTo(@10);
    }];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.marginView);
        make.leading.equalTo(self.mv_imageView).offset(20);
    }];
    
    [self.groupView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.marginView.mas_bottom);
        make.leading.trailing.bottom.equalTo(self.view);
    }];
}

#pragma mark
#pragma mark -- collection 代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}

- (RepeatViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RepeatViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"repeat" forIndexPath:indexPath];
    
    cell.imageUrl = [NSURL URLWithString:self.imageArray[indexPath.item % _imageArray.count]];
    
    return cell;
}

- (void)addbtn{
    
    CGFloat btnW = (self.view.frame.size.width - margin * 2) / 3;
    
    CGFloat groupH = self.view.frame.size.height - 64 - self.view.frame.size.height*0.5;
    
    CGFloat btnH = (groupH - margin * 2) / 2;
    
    for (int i = 0; i < 6; i++) {
        
        NSInteger row = i / kBtnCount;
        NSInteger col = i % kBtnCount;
       //改成button啦组长
        UIButton *tempButton = [[UIButton alloc] init];
        
        CGFloat x = (margin + btnW) * col + margin;
        
        CGFloat y = (margin + btnH) * row + margin;
        
        tempButton.frame = CGRectMake(x, y, btnW, btnH);
        
        tempButton.backgroundColor = [UIColor whiteColor];
        
        [self.groupView addSubview:tempButton];
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        BtnModel *model = [BtnModel modelList][i];
        imageView.image = [UIImage imageNamed:model.icon];
        
        [tempButton addSubview:imageView];
        
        UILabel *tempLabel = [[UILabel alloc] init];
        
        [tempLabel setText:model.name];
        tempButton.titleLabel.text = model.name;
        [tempLabel setFont:[UIFont systemFontOfSize:20]];
        
        [tempButton addSubview:tempLabel];
        
#pragma
#pragma mark -- 设置约束
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(tempButton);
            make.top.equalTo(tempButton).offset(23);
        }];
        
        [tempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(tempButton);
            make.top.equalTo(imageView.mas_bottom).offset(13);
        }];
        
        if (i>= 0 && i < 5) {
          [tempButton addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (i == 5) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(public:)];
            [tempButton addGestureRecognizer:tap];
        }
    }
}

#pragma mark
#pragma mark -- 点击tempView 实现跳转界面
- (void)public:(UITapGestureRecognizer *)tap{
    
    PublicBenefitViewController *PbVc = [[PublicBenefitViewController alloc] init];
    
    [self.navigationController pushViewController:PbVc animated:YES];
}

- (void)tap:(UIButton *)tap{
    self.titleStr = tap.titleLabel.text;

#pragma
#pragma mark -- 实现界面跳转
    TumourViewController *tumourVC =[[TumourViewController alloc] init];
    tumourVC.str = tap.titleLabel.text;
    [self.navigationController pushViewController:tumourVC animated:YES];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //3.滚动结束之后,调回到中间section对应的item位置
    
    //contentoffset / 屏幕的宽度
    int pageIndex =  scrollView.contentOffset.x / self.repeatCollectionView.frame.size.width;
    
    //模运算
    pageIndex = pageIndex % self.imageArray.count;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageIndex inSection:1];
    [self.repeatCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSInteger pageIndex =  scrollView.contentOffset.x / self.repeatCollectionView.frame.size.width;
    
    //模运算
    pageIndex = pageIndex % self.imageArray.count;
    
    //pageIndex
    self.pageControl.currentPage = pageIndex;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageIndex inSection:1];
    [self.repeatCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

//将要开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    self.timer.fireDate = [NSDate distantFuture];
}
//停止拖拽

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
}
@end

